﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using TimeLineDataContract;

namespace TimeLineControlPowerPointAddIn
{
    public partial class RibbonTimeLineControl
    {
        #region Fields Declarations;

        internal SlideDeskContract DataWizardResult;

        #endregion

        #region Ribbom controls Events

        private void EditBoxSlideTimeTextChanged(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            var timeslide = editBoxSlideTime.Text.Split(new[] { ':' });
            if (timeslide.Length != 3)
            {
                MessageBox.Show(ResMng.StringResource("MsgErrSlideTime"), ResMng.StringResource("NameApp"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                editBoxSlideTime.Text = "00:00:00";
            }
            else
            {
                try
                {
                    TimeSpan.Parse(editBoxSlideTime.Text);
                }
                catch
                {
                    MessageBox.Show(ResMng.StringResource("MsgErrSlideTime"), ResMng.StringResource("NameApp"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    editBoxSlideTime.Text = "00:00:00";
                }
            }
            if (editBoxSlideTime.Text != editBoxSlideTime.Tag.ToString())
            {
                BntOkTime.Tag = "1";
                BntOkTime.Image = Properties.Resources.changed;
            }
        }

        private void EditBoxSlideTitleTextChanged(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            editBoxSlideTitle.Tag = editBoxSlideTitle.Text.Trim();
            if (editBoxSlideTitle.Text.Length == 0)
            {
                editBoxSlideTitle.Text = editBoxSlideTitle.Tag.ToString();
            }
            if (editBoxSlideTitle.Text != editBoxSlideTitle.Tag.ToString())
            {
                BntTitle.Image = Properties.Resources.changed;
                BntTitle.Tag = "1";
            }
        }

        private void ChkFullScreen_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.Application.Presentations.Count == 0)
            {
                MessageBox.Show(ResMng.StringResource("MsgNoActPres"), ResMng.StringResource("NameApp"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].FullScreen =
                ChkFullScreen.Checked;
            ThisAddIn.SendDeckToTimeLineControl = true;
        }

        private void DropDownPositionSelectionChanged(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.Application.Presentations.Count == 0)
            {
                MessageBox.Show(ResMng.StringResource("MsgNoActPres"), ResMng.StringResource("NameApp"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            if (dropDownPosition.Items[dropDownPosition.SelectedItemIndex].Tag.ToString() == "T")
            {
                ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].DockPosition =
                    DockWindows.Top;
            }
            else if (dropDownPosition.Items[dropDownPosition.SelectedItemIndex].Tag.ToString() == "B")
            {
                ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].DockPosition =
                    DockWindows.Bottom;
            }
            ThisAddIn.SendDeckToTimeLineControl = true;
        }

        private void BntShowTimeBar_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            var cur = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            SuspendLayout();
            if (BntSHowTimeBar.Tag.ToString() == "1")
            {
                BntSHowTimeBar.Image = Properties.Resources.LUZVERM0;
                BntSHowTimeBar.Tag = "0";
                BntSHowTimeBar.Label = ResMng.StringResource("TimeBarOFF");
                try
                {
                    TimeLineControlNamedPipes.SenderPipe.SendTimeLineShowBar(false, ThisAddIn.CurrentPipeNameOut);
                    ThisAddIn.TimeLineBarVisible = false;
                }
                catch
                {
                    BntSHowTimeBar.Image = Properties.Resources.LUZCINZ0;
                    BntSHowTimeBar.Tag = "1";
                    BntSHowTimeBar.Label = ResMng.StringResource("TimeBarON");
                    ThisAddIn.EnabledRibGroupTimeLineControl(false, this);
                }
            }
            else if (BntSHowTimeBar.Tag.ToString() == "0")
            {
                BntSHowTimeBar.Image = Properties.Resources.LUZVERD0;
                BntSHowTimeBar.Tag = "1";
                BntSHowTimeBar.Label = ResMng.StringResource("TimeBarON");
                try
                {
                    TimeLineControlNamedPipes.SenderPipe.SendTimeLineShowBar(true, ThisAddIn.CurrentPipeNameOut);
                    ThisAddIn.TimeLineBarVisible = true;
                }
                catch
                {
                    BntSHowTimeBar.Image = Properties.Resources.LUZCINZ0;
                    BntSHowTimeBar.Tag = "0";
                    BntSHowTimeBar.Label = ResMng.StringResource("TimeBarOFF");
                    ThisAddIn.EnabledRibGroupTimeLineControl(false, this);
                }
            }
            ResumeLayout(true);
            Cursor.Current = cur;
        }

        private void BntOkTime_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            editBoxSlideTime.Tag = editBoxSlideTime.Text;
            BntOkTime.Image = Properties.Resources.icn_troca;
            BntOkTime.Tag = null;
            if (Globals.ThisAddIn.Application.Presentations.Count == 0)
            {
                MessageBox.Show(ResMng.StringResource("MsgNoActPres"), ResMng.StringResource("NameApp"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].ElaspTime = TimeSpan.Parse(editBoxSlideTime.Text);
            SuspendLayout();
            LblTotTime.Label = ThisAddIn.TimeSpanFormat(ThisAddIn.TotalTimePresentation(Globals.ThisAddIn.Application.ActivePresentation));
            ResumeLayout(true);
            ThisAddIn.SendDeckToTimeLineControl = true;
        }

        private void BntTitle_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            editBoxSlideTitle.Tag = editBoxSlideTitle.Text;
            BntTitle.Image = Properties.Resources.icn_troca;
            BntTitle.Tag = null;
            if (Globals.ThisAddIn.Application.Presentations.Count == 0)
            {
                MessageBox.Show(ResMng.StringResource("MsgNoActPres"), ResMng.StringResource("NameApp"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].Title = editBoxSlideTitle.Text;
            ThisAddIn.SendDeckToTimeLineControl = true;
        }

        private void BntDataWizard_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.Application.Presentations.Count == 0)
            {
                MessageBox.Show(ResMng.StringResource("MsgNoActPres"), ResMng.StringResource("NameApp"), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            var cur = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            DataWizardResult = null;

            var slide = (Slide)Globals.ThisAddIn.Application.ActiveWindow.View.Slide;
            var sugestText = new List<string>();
            for (var i = 1; i < slide.Shapes.Count+1; i++)
            {
                var shape = slide.Shapes[i];
                if (shape.HasTextFrame == MsoTriState.msoTrue)
                {
                    if (shape.TextFrame.HasText == MsoTriState.msoTrue)
                    {
                        var textRange = shape.TextFrame.TextRange;
                        var text = textRange.Text.Split('\v');
                        sugestText.AddRange(text);
                        if (i >= 2)
                        {
                            break;
                        }
                    }
                }
            }
            var frm = new FrmDataTimeLineControl(ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)], sugestText.ToArray());
            Cursor.Current = cur;
            frm.ShowDialog();
            if (DataWizardResult != null)
            {
                editBoxSlideTime.Text = DataWizardResult.ElaspTime.ToString("c");
                editBoxSlideTime.Tag = editBoxSlideTime.Text;
                BntOkTime.Image = Properties.Resources.icn_troca;
                BntOkTime.Tag = null;
                ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].ElaspTime = TimeSpan.Parse(editBoxSlideTime.Text);

                editBoxSlideTitle.Text = DataWizardResult.Title;
                editBoxSlideTitle.Tag = editBoxSlideTitle.Text;
                BntTitle.Image = Properties.Resources.icn_troca;
                BntTitle.Tag = null;
                ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].Title = editBoxSlideTitle.Text;

                if (DataWizardResult.DockPosition == DockWindows.Top)
                {
                    dropDownPosition.SelectedItemIndex = 0;
                    ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].DockPosition =
                        DockWindows.Top;
                }
                else if (DataWizardResult.DockPosition == DockWindows.Bottom)
                {
                    dropDownPosition.SelectedItemIndex = 1;
                    ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].DockPosition =
                        DockWindows.Bottom;
                }

                ChkFullScreen.Checked = DataWizardResult.FullScreen;
                ThisAddIn.DicSlides[ThisAddIn.CurrentAppName][long.Parse(LblSlideID.Label)].FullScreen = ChkFullScreen.Checked;

                LblTotTime.Label = ThisAddIn.TimeSpanFormat(ThisAddIn.TotalTimePresentation(Globals.ThisAddIn.Application.ActivePresentation));

                ThisAddIn.SendDeckToTimeLineControl = true;
            }
        }

        #endregion

        #region Ribbom Load/close

        // ReSharper disable UnusedParameter.Local
        private void RibbonTimeLineControl_Load(object sender, Microsoft.Office.Tools.Ribbon.RibbonUIEventArgs e)
        {
            dropDownPosition.Items.Add(Factory.CreateRibbonDropDownItem());
            dropDownPosition.Items.Add(Factory.CreateRibbonDropDownItem());
            SuspendLayout();
            dropDownPosition.Items[0].Label = ResMng.StringResource("ItemTop");
            dropDownPosition.Items[0].Tag = "T";
            dropDownPosition.Items[1].Label = ResMng.StringResource("ItemBottom");
            dropDownPosition.Items[1].Tag = "B";
            ResumeLayout(true);

            LblSlideID.Label = string.Empty;
            BntEnableTimeLine.Tag = string.Empty;

        }
        // ReSharper restore UnusedParameter.Local
        #endregion
    }
}
